﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.Distribution;
using Abp.Runtime.Caching;
using Abp.RemoteEventBus;
using Abp.BackgroundJobs;
using FwProject.BackGround.Settlement;
using FwProject.BackGround;
using Abp.Timing;
using FwProject.Comm;
using Abp.Domain.Uow;

namespace FwProject.Application.EventHandl
{
    public class SettlementInfoHandle: IEventHandler<EntityCreatedEventData<SettlementInfo>>,
    IEventHandler<EntityChangedEventData<SettlementInfo>>, ITransientDependency
    {
        private readonly IRepository<DistributionInfo, Guid> _distributionInfo;
        private readonly IRepository<DistributionInfoDetails, Guid> _distributionInfoDetails;
        public readonly ICacheManager _catchmanager;
        private readonly IBackgroundJobManager _backgroundJobManager;
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public SettlementInfoHandle(ICacheManager catchmanager,IRepository<DistributionInfo, Guid> distributionInfo,
                                    IRepository<DistributionInfoDetails, Guid> distributionInfoDetails , IBackgroundJobManager backgroundJobManager)
        {

            _catchmanager = catchmanager;
            _distributionInfo = distributionInfo;
            _distributionInfoDetails = distributionInfoDetails;
            remoteEventBus = NullRemoteEventBus.Instance;
            _backgroundJobManager = backgroundJobManager;
        
        }
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="eventData">Event data.</param>
        [UnitOfWork]
        public void HandleEvent(EntityChangedEventData<SettlementInfo> eventData)
        {
            ICache cache = _catchmanager.GetCache(nameof(SettlementInfo));
            cache.Remove("list");

            cache = _catchmanager.GetCache(nameof(DistributionInfo));
            cache.Clear();
            cache = _catchmanager.GetCache(nameof(DistributionInfoDetails));
            cache.Clear();

            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TypeModelCatch)
            {
                Data = { [RemoteTypeHelp.Model] = nameof(SettlementInfo) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TopicSystemCatch, catcheventDate);
            ///生成收款单据
            //if (eventData.Entity.RestaurantPayStatus != RestaurantPayStatus.作废 || eventData.Entity.RestaurantPayStatus != RestaurantPayStatus.坏账确认)
            //{
            //    //延迟通知司机有新的单据需要收款，如果已收款通知餐厅端司机收款带确认
             
            //}

        }
        [UnitOfWork]
        public void HandleEvent(EntityCreatedEventData<SettlementInfo> eventData)
        {
            try
            {
          
               
            }
            catch (Exception)
            {
            }
           

        }
    }
}
